package com.cyt.web.controller.sys;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.vo.SysFileVo;
import com.cyt.sys.service.SysFileService;
import com.cyt.sys.service.SysUserService;
import com.cyt.web.controller.base.PhoneController;
import com.cyt.wx.model.po.WxMember;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import sun.misc.BASE64Decoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;

import static com.cyt.web.controller.base.BaseController.acceptCurrentUser;

/**
 * @Author: YuShengQiang
 * @Date: 2018-12-04 10:34
 * @Description:基础附件控制层
 */
@RestController
public class SysFileController extends PhoneController {

    @Resource
    private SysFileService sysFileService;

    @Resource
    private SysUserService sysUserService;

    /**
     * @Author: YuShengQiang
     * @Date: 2018-11-23 9:19
     * @Description:附件管理统一跳转处理
     */
    @RequestMapping("/base/sysFile/*")
    public Model goPage(Model model) {
        return model;
    }

    /**
     * 单个附件上传
     *
     * @param objectId objectId
     * @return
     * @author shengQiang yu
     * @date 2019-03-08 13:59
     */
    @RequestMapping("/base/sysFile/singleFileUpload")
    public ModelAndView goImportPage(String objectId) {
        ModelAndView modelAndView = new ModelAndView("/base/sysFile/singleFile");
        modelAndView.addObject("objectId", objectId);
        return modelAndView;
    }
    /**
     * @Author: YuShengQiang
     * @Date: 2018-12-04 10:35
     * @Description: 裁剪图片上传
     */
    @BaseLog(module = "系统模块", classz = "附件管理", methods = "裁剪图片上传", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysFile/uploadImageData", method = RequestMethod.POST)
    public ReturnResult uploadImageData(String imgBase64, HttpServletRequest request, String catalog, String objectId) throws UnsupportedEncodingException {
        String img= URLDecoder.decode(imgBase64,"UTF-8");
        String imgType=img.substring(img.indexOf("/")+1,img.indexOf(";"));
        String imgStr=img.substring(img.indexOf(",")+1);
        SysUser sysUser = acceptCurrentUser(request);
        return sysFileService.imageBase64Upload(imgStr, request, catalog, imgType,objectId, sysUser.getId());
    }
    /**
     * @Author: YuShengQiang
     * @Date: 2018-12-04 10:35
     * @Description: 图片上传
     */
    @BaseLog(module = "系统模块", classz = "附件管理", methods = "单个图片上传", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysFile/singleImage", method = RequestMethod.POST)
    public ReturnResult singleImage(MultipartFile file, HttpServletRequest request, String catalog, String objectId) {
        SysUser sysUser = acceptCurrentUser(request);
        return sysFileService.singleImage(file, request, catalog, objectId, sysUser.getId());
    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-12-04 10:35
     * @Description: 图片上传
     */
    @BaseLog(module = "系统模块", classz = "附件管理", methods = "单个图片上传", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysFile/singleImageNew", method = RequestMethod.POST)
    public ReturnResult singleImageNew(MultipartFile file, HttpServletRequest request, String catalog, String objectId, String token) {
        //SysUser baseUser=sysUserService.selectUserByToken(token);
        WxMember wxMember = acceptToken(token);
        return sysFileService.singleImage(file, request, catalog, objectId, wxMember.getId());
    }

    /**
     * @Author: cuibiming
     * @Date: 2019-12-12 10:35
     * @Description: 未登录用户图片上传
     */
    @BaseLog(module = "系统模块", classz = "附件管理", methods = "未登录用户单个图片上传", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysFile/singleImageNoMerber", method = RequestMethod.POST)
    public ReturnResult singleImageNoMember(MultipartFile file, HttpServletRequest request, String catalog, String objectId, String taskId) {
        //SysUser baseUser=sysUserService.selectUserByToken(token);
        //WxMember wxMember = acceptToken(token);
        return sysFileService.singleImage(file, request, catalog, objectId, taskId);
    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-12-04 10:35
     * @Description: 附件上传
     */
    @BaseLog(module = "系统模块", classz = "附件管理", methods = "单个附件上传", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysFile/singleFile", method = RequestMethod.POST)
    public ReturnResult singleFile(MultipartFile file, HttpServletRequest request, String catalog, String objectId) {
        SysUser sysUser = acceptCurrentUser(request);
        return sysFileService.singleFile(file, request, catalog, objectId, sysUser.getId());
    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-12-04 10:35
     * @Description: 根据id删除附件
     */
    @BaseLog(module = "系统模块", classz = "附件管理", methods = "根据id删除附件", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysFile/deleteById", method = RequestMethod.POST)
    public ReturnResult deleteById(HttpServletRequest request, String id) {
        return sysFileService.deleteById(request, id);
    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-12-04 10:35
     * @Description: 根据objectId删除附件
     */
    @BaseLog(module = "系统模块", classz = "附件管理", methods = "根据objectId删除附件", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysFile/deleteByobjectId", method = RequestMethod.POST)
    public ReturnResult deleteByobjectId(String objectId) {
        return sysFileService.deleteByobjectId(objectId);
    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-12-04 10:35
     * @Description: 文本编辑器图片接口
     */
    @BaseLog(module = "系统模块", classz = "附件管理", methods = "文本编辑器图片上传", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysFile/layEditUpload", method = RequestMethod.POST)
    public ReturnResult layEditUpload(MultipartFile file, HttpServletRequest request, String catalog) {
        SysUser sysUser = acceptCurrentUser(request);
        return sysFileService.layEditUpload(file, request, catalog, "", sysUser.getId());
    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-12-04 10:35
     * @Description: 附件查询
     */
    @BaseLog(module = "系统模块", classz = "附件管理", methods = "附件查询", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysFile/list", method = RequestMethod.POST)
    public ReturnResult list(SysFileVo sysFileVo) {
        return sysFileService.list(sysFileVo);
    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-12-04 10:35
     * @Description: 附件下载
     */
    @BaseLog(module = "系统模块", classz = "附件管理", methods = "附件下载", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysFile/downloadById")
    public void download(HttpServletResponse response, HttpServletRequest request, String id) {
        sysFileService.download(response, request, id);
    }

    /**
     * 根据附件objectId预览/下载第一个附件
     *
     * @param objectId 附件objectId
     * @return
     * @author shengQiang yu
     * @date 2019-06-26 16:36
     */
    @BaseLog(module = "系统模块", classz = "附件管理", methods = "根据附件objectId预览/下载第一个附件", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysFile/showFileByObjectId", method = RequestMethod.GET)
    public void showFileByObjectId(HttpServletResponse response, HttpServletRequest request, String objectId) throws IOException {
        sysFileService.showFileByObjectId(response, request, objectId);
    }

    /**
     * 根据附件id预览/下载附件
     *
     * @param fileId 附件id
     * @return
     * @author shengQiang yu
     * @date 2019-06-26 16:36
     */
    @BaseLog(module = "系统模块", classz = "附件管理", methods = "根据附件id预览/下载附件", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysFile/showFileById", method = RequestMethod.GET)
    public void showFileById(HttpServletResponse response, HttpServletRequest request, String fileId) throws IOException {
        sysFileService.showFileById(response, request, fileId);
    }

    /**
     * 根据路径预览/下载附件
     *
     * @param path 附件路径
     * @return
     * @author shengQiang yu
     * @date 2019-06-26 16:36
     */
    @BaseLog(module = "系统模块", classz = "附件管理", methods = "根据路径预览/下载附件", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysFile/showFileByPath", method = RequestMethod.GET)
    public void showFileByPath(HttpServletResponse response, HttpServletRequest request, String path) throws IOException {
        sysFileService.showFileByPath(response, request, path);
    }

    /**
     * office 转成pdf
     *
     * @param request
     * @param id      附件id
     * @Author: sunli
     * @Date: 201９-04-12
     */
    @BaseLog(module = "系统模块", classz = "附件管理", methods = "附件预览", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysFile/tranPdf")
    public ReturnResult officeToPdf(HttpServletRequest request, String id) {
        return sysFileService.conversionFile(request, id);
    }


    /**
     * 在线预览
     *
     * @param request
     * @param response
     * @param path     处理之后的pdf地址
     * @throws Exception
     * @Author: sunli
     * @Date: 201９-04-12
     */
    @RequestMapping(value = "/sysFile/preview")
    @ResponseBody
    public void devDoc(HttpServletRequest request, HttpServletResponse response, String path) throws Exception {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/pdf");
        FileInputStream in = new FileInputStream(new File(path));
        OutputStream out = response.getOutputStream();
        byte[] b = new byte[512];
        while ((in.read(b)) != -1) {
            out.write(b);
        }
        out.flush();
        in.close();
        out.close();
    }
}
